import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';

void main() {
  MediaKit.ensureInitialized();

  return runApp(MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final player = Player();
  late final videoController = VideoController(player);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(children: [
          // 播放器
          Expanded(
              child: Video(
            controller: videoController,
            subtitleViewConfiguration: const SubtitleViewConfiguration(
              style: TextStyle(
                height: 1.4,
                fontSize: 24.0,
                letterSpacing: 0.0,
                wordSpacing: 0.0,
                color: Color(0xffffffff),
                fontWeight: FontWeight.normal,
                backgroundColor: Color(0xaa000000),
              ),
              textAlign: TextAlign.center,
              padding: EdgeInsets.all(24.0),
            ),
          )),
          // 测试按钮
          Container(
            height: 100,
            child: Row(
              children: [
                TextButton(
                    onPressed: () {
                      test("open");
                    },
                    child: Text("打开并播放视频")),
                TextButton(
                    onPressed: () {
                      test("stop");
                    },
                    child: Text("停止播放")),
                TextButton(
                    onPressed: () {
                      test("play");
                    },
                    child: Text("开始播放")),
                TextButton(
                    onPressed: () {
                      test("playOrPause");
                    },
                    child: Text("切换播放")),
                TextButton(
                    onPressed: () {
                      test("pause");
                    },
                    child: Text("暂停")),
                TextButton(
                    onPressed: () {
                      test("seek");
                    },
                    child: Text("跳转")),
              ],
            ),
          )
        ]),
      ),
    );
  }

  void test(String op) {
    switch (op) {
      case "open":
        print("打开并播放");
        String url = "/Users/qiang/英语/译林英语/unit1/unit1storytime.mp4";
        player.open(Media(url));
        break;
      case "stop":
        print("停止播放");
        player.stop();
        break;
      case "play":
        print("开始播放");
        player.play();
        break;
      case "playOrPause":
        print("暂停或继续");
        player.playOrPause();
        break;
      case "pause":
        print("暂停");
        player.pause();
        break;
      case "seek":
        print("跳转60秒");
        // player.seek(Duration(milliseconds: 60));
        player.seek(Duration(seconds: 60));
        // player.play();
        break;
    }
  }
}
